{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from base_helper import *\n",
    "op_rd1 = get_operation_round1_new()\n",
    "op_tr = get_operation_train_new()\n",
    "tag = get_tag_train_new()\n",
    "sub = get_sub()\n",
    "sub[tag_hd.Tag] = -1\n",
    "# 测试集没有tag \n",
    "op_rd1 = sub.merge(op_rd1, on='UID', how='left')\n",
    "op_tr = tag.merge(op_tr, on='UID', how='left')\n",
    "cols = operation_header + [tag_hd.Tag]\n",
    "op_merge = pd.concat([op_tr[cols],op_rd1[cols]])\n",
    "del op_tr,op_rd1 \n",
    "op_merge = op_merge.fillna(method=\"ffill\")\n",
    "# 后向填充，使用下一行的值,不存在的时候就不填充\n",
    "op_merge = op_merge.fillna(method=\"bfill\") \n",
    "op_merge = fill_mean(op_merge)\n",
    "op_merge.fillna(-1, inplace=True)\n",
    "op_merge.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "      <th>day_uid_cnt</th>\n",
       "      <th>day_op_cnt_rate</th>\n",
       "      <th>day_uid_nunique</th>\n",
       "      <th>day_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>141285</td>\n",
       "      <td>0.096619</td>\n",
       "      <td>14496</td>\n",
       "      <td>0.402494</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   day  day_uid_cnt  day_op_cnt_rate  day_uid_nunique  day_op_nunique_rate\n",
       "0  1.0       141285         0.096619            14496             0.402494"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用户所在的天数之和\n",
    "day_gb = op_merge.groupby(op_hd.day) \n",
    "day_st = day_gb[tag_hd.UID].count().reset_index()\n",
    "day_st.columns = ['day','day_uid_cnt']\n",
    "day_st['day_op_cnt_rate'] =day_st['day_uid_cnt']/op_train.shape[0]\n",
    "day_st02 = day_gb[tag_hd.UID].nunique().reset_index()\n",
    "day_st02.columns = ['day','day_uid_nunique']\n",
    "day_st02['day_op_nunique_rate'] =day_st['day_uid_cnt']/day_st02['day_uid_nunique'].sum()\n",
    "day_st = day_st.merge(day_st02, on=op_hd.day, how='left')\n",
    "day_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mode</th>\n",
       "      <th>mode_uid_cnt</th>\n",
       "      <th>mode_op_cnt_rate</th>\n",
       "      <th>mode_uid_nunique</th>\n",
       "      <th>mode_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00094ae2a1d62504</td>\n",
       "      <td>56996</td>\n",
       "      <td>0.038977</td>\n",
       "      <td>8575</td>\n",
       "      <td>0.138543</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               mode  mode_uid_cnt  mode_op_cnt_rate  mode_uid_nunique  \\\n",
       "0  00094ae2a1d62504         56996          0.038977              8575   \n",
       "\n",
       "   mode_op_nunique_rate  \n",
       "0              0.138543  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用户操作的操作模型次数之和\n",
    "mode_gb = op_merge.groupby(op_hd.mode) \n",
    "mode_st = mode_gb[tag_hd.UID].count().reset_index()\n",
    "mode_st.columns = ['mode','mode_uid_cnt']\n",
    "mode_st['mode_op_cnt_rate'] =mode_st['mode_uid_cnt']/op_train.shape[0]\n",
    "mode_st02 = mode_gb[tag_hd.UID].nunique().reset_index()\n",
    "mode_st02.columns = ['mode','mode_uid_nunique']\n",
    "mode_st02['mode_op_nunique_rate'] =mode_st['mode_uid_cnt']/mode_st02['mode_uid_nunique'].sum()\n",
    "mode_st = mode_st.merge(mode_st02, on=op_hd.mode, how='left')\n",
    "mode_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>success</th>\n",
       "      <th>success_uid_cnt</th>\n",
       "      <th>success_op_cnt_rate</th>\n",
       "      <th>success_uid_nunique</th>\n",
       "      <th>success_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>162130</td>\n",
       "      <td>0.110874</td>\n",
       "      <td>34446</td>\n",
       "      <td>1.683663</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   success  success_uid_cnt  success_op_cnt_rate  success_uid_nunique  \\\n",
       "0      0.0           162130             0.110874                34446   \n",
       "\n",
       "   success_op_nunique_rate  \n",
       "0                 1.683663  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不同操作状态下的操作数量，与比值\n",
    "success_gb = op_merge.groupby(op_hd.success) \n",
    "success_st = success_gb[tag_hd.UID].count().reset_index()\n",
    "success_st.columns = ['success','success_uid_cnt']\n",
    "success_st['success_op_cnt_rate'] =success_st['success_uid_cnt']/op_train.shape[0]\n",
    "success_st02 = success_gb[tag_hd.UID].nunique().reset_index()\n",
    "success_st02.columns = ['success','success_uid_nunique']\n",
    "success_st02['success_op_nunique_rate'] =success_st['success_uid_cnt']/success_st02['success_uid_nunique'].sum()\n",
    "success_st = success_st.merge(success_st02, on=op_hd.success, how='left')\n",
    "success_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>UID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00</td>\n",
       "      <td>46386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>01</td>\n",
       "      <td>19324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>02</td>\n",
       "      <td>11108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>03</td>\n",
       "      <td>7893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>04</td>\n",
       "      <td>11502</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>05</td>\n",
       "      <td>27442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>06</td>\n",
       "      <td>71362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>07</td>\n",
       "      <td>137580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>08</td>\n",
       "      <td>223639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>09</td>\n",
       "      <td>253039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>260270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>11</td>\n",
       "      <td>223576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>12</td>\n",
       "      <td>198462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>13</td>\n",
       "      <td>165967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>14</td>\n",
       "      <td>173514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>15</td>\n",
       "      <td>191673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>16</td>\n",
       "      <td>196026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>17</td>\n",
       "      <td>197156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>18</td>\n",
       "      <td>186403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>19</td>\n",
       "      <td>168938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>20</td>\n",
       "      <td>158173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>21</td>\n",
       "      <td>138627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>22</td>\n",
       "      <td>101491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>23</td>\n",
       "      <td>65103</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   time     UID\n",
       "0    00   46386\n",
       "1    01   19324\n",
       "2    02   11108\n",
       "3    03    7893\n",
       "4    04   11502\n",
       "5    05   27442\n",
       "6    06   71362\n",
       "7    07  137580\n",
       "8    08  223639\n",
       "9    09  253039\n",
       "10   10  260270\n",
       "11   11  223576\n",
       "12   12  198462\n",
       "13   13  165967\n",
       "14   14  173514\n",
       "15   15  191673\n",
       "16   16  196026\n",
       "17   17  197156\n",
       "18   18  186403\n",
       "19   19  168938\n",
       "20   20  158173\n",
       "21   21  138627\n",
       "22   22  101491\n",
       "23   23   65103"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 操作日期\n",
    "op_merge[op_hd.time] = op_merge[op_hd.time].map(lambda x:x[:2])\n",
    "op_merge.groupby(op_hd.time)[tag_hd.UID].count().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>time_uid_cnt</th>\n",
       "      <th>time_op_cnt_rate</th>\n",
       "      <th>time_uid_nunique</th>\n",
       "      <th>time_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00</td>\n",
       "      <td>46386</td>\n",
       "      <td>0.031721</td>\n",
       "      <td>5488</td>\n",
       "      <td>0.140886</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  time  time_uid_cnt  time_op_cnt_rate  time_uid_nunique  time_op_nunique_rate\n",
       "0   00         46386          0.031721              5488              0.140886"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 操作时间点\n",
    "time_gb = op_merge.groupby(op_hd.time)\n",
    "time_st = time_gb[tag_hd.UID].count().reset_index()\n",
    "time_st.columns = ['time','time_uid_cnt']\n",
    "time_st['time_op_cnt_rate'] =time_st['time_uid_cnt']/op_train.shape[0]\n",
    "time_st02 = time_gb[tag_hd.UID].nunique().reset_index()\n",
    "time_st02.columns = ['time','time_uid_nunique']\n",
    "time_st02['time_op_nunique_rate'] =time_st['time_uid_cnt']/time_st02['time_uid_nunique'].sum()\n",
    "time_st = time_st.merge(time_st02, on=op_hd.time, how='left')\n",
    "time_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>os</th>\n",
       "      <th>os_uid_cnt</th>\n",
       "      <th>os_op_cnt_rate</th>\n",
       "      <th>os_uid_nunique</th>\n",
       "      <th>os_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>101.0</td>\n",
       "      <td>16771</td>\n",
       "      <td>0.011469</td>\n",
       "      <td>8603</td>\n",
       "      <td>0.157409</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      os  os_uid_cnt  os_op_cnt_rate  os_uid_nunique  os_op_nunique_rate\n",
       "0  101.0       16771        0.011469            8603            0.157409"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 操作系统\n",
    "os_gb = op_merge.groupby(op_hd.os)\n",
    "os_st = os_gb[tag_hd.UID].count().reset_index()\n",
    "os_st.columns = ['os','os_uid_cnt']\n",
    "os_st['os_op_cnt_rate'] =os_st['os_uid_cnt']/op_train.shape[0]\n",
    "os_st02 = os_gb[tag_hd.UID].nunique().reset_index()\n",
    "os_st02.columns = ['os','os_uid_nunique']\n",
    "os_st02['os_op_nunique_rate'] =os_st['os_uid_cnt']/os_st02['os_uid_nunique'].sum()\n",
    "os_st = os_st.merge(os_st02, on=op_hd.os, how='left')\n",
    "os_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>version</th>\n",
       "      <th>version_uid_cnt</th>\n",
       "      <th>version_op_cnt_rate</th>\n",
       "      <th>version_uid_nunique</th>\n",
       "      <th>version_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0.2</td>\n",
       "      <td>9</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000091</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  version  version_uid_cnt  version_op_cnt_rate  version_uid_nunique  \\\n",
       "0   0.0.2                9             0.000006                    2   \n",
       "\n",
       "   version_op_nunique_rate  \n",
       "0                 0.000091  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 客户端版本号\n",
    "version_gb = op_merge.groupby(op_hd.version)\n",
    "version_st = version_gb[tag_hd.UID].count().reset_index()\n",
    "version_st.columns = ['version','version_uid_cnt']\n",
    "version_st['version_op_cnt_rate'] =version_st['version_uid_cnt']/op_train.shape[0]\n",
    "version_st02 = version_gb[tag_hd.UID].nunique().reset_index()\n",
    "version_st02.columns = ['version','version_uid_nunique']\n",
    "version_st02['version_op_nunique_rate'] =version_st['version_uid_cnt']/version_st02['version_uid_nunique'].sum()\n",
    "version_st = version_st.merge(version_st02, on=op_hd.version, how='left')\n",
    "version_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>device1</th>\n",
       "      <th>device1_uid_cnt</th>\n",
       "      <th>device1_op_cnt_rate</th>\n",
       "      <th>device1_uid_nunique</th>\n",
       "      <th>device1_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00271009e466f04a</td>\n",
       "      <td>142</td>\n",
       "      <td>0.000097</td>\n",
       "      <td>1</td>\n",
       "      <td>0.001846</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            device1  device1_uid_cnt  device1_op_cnt_rate  \\\n",
       "0  00271009e466f04a              142             0.000097   \n",
       "\n",
       "   device1_uid_nunique  device1_op_nunique_rate  \n",
       "0                    1                 0.001846  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 操作设备参数1\n",
    "device1_gb = op_merge.groupby(op_hd.device1)\n",
    "device1_st = device1_gb[tag_hd.UID].count().reset_index()\n",
    "device1_st.columns = ['device1','device1_uid_cnt']\n",
    "device1_st['device1_op_cnt_rate'] =device1_st['device1_uid_cnt']/op_train.shape[0]\n",
    "device1_st02 = device1_gb[tag_hd.UID].nunique().reset_index()\n",
    "device1_st02.columns = ['device1','device1_uid_nunique']\n",
    "device1_st02['device1_op_nunique_rate'] =device1_st['device1_uid_cnt']/device1_st02['device1_uid_nunique'].sum()\n",
    "device1_st = device1_st.merge(device1_st02, on=op_hd.device1, how='left')\n",
    "device1_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>device2</th>\n",
       "      <th>device2_uid_cnt</th>\n",
       "      <th>device2_op_cnt_rate</th>\n",
       "      <th>device2_uid_nunique</th>\n",
       "      <th>device2_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1105</td>\n",
       "      <td>814</td>\n",
       "      <td>0.000557</td>\n",
       "      <td>22</td>\n",
       "      <td>0.009432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  device2  device2_uid_cnt  device2_op_cnt_rate  device2_uid_nunique  \\\n",
       "0    1105              814             0.000557                   22   \n",
       "\n",
       "   device2_op_nunique_rate  \n",
       "0                 0.009432  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "device2_gb = op_merge.groupby(op_hd.device2)\n",
    "device2_st = device2_gb[tag_hd.UID].count().reset_index()\n",
    "device2_st.columns = ['device2','device2_uid_cnt']\n",
    "device2_st['device2_op_cnt_rate'] =device2_st['device2_uid_cnt']/op_train.shape[0]\n",
    "device2_st02 = device2_gb[tag_hd.UID].nunique().reset_index()\n",
    "device2_st02.columns = ['device2','device2_uid_nunique']\n",
    "device2_st02['device2_op_nunique_rate'] =device2_st['device2_uid_cnt']/device2_st02['device2_uid_nunique'].sum()\n",
    "device2_st = device2_st.merge(device2_st02, on=op_hd.device2, how='left')\n",
    "device2_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>device_code1</th>\n",
       "      <th>device_code1_uid_cnt</th>\n",
       "      <th>device_code1_op_cnt_rate</th>\n",
       "      <th>device_code1_uid_nunique</th>\n",
       "      <th>device_code1_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0002150cffad8bee</td>\n",
       "      <td>79</td>\n",
       "      <td>0.000054</td>\n",
       "      <td>2</td>\n",
       "      <td>0.001037</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       device_code1  device_code1_uid_cnt  device_code1_op_cnt_rate  \\\n",
       "0  0002150cffad8bee                    79                  0.000054   \n",
       "\n",
       "   device_code1_uid_nunique  device_code1_op_nunique_rate  \n",
       "0                         2                      0.001037  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "device_code1_gb = op_merge.groupby(op_hd.device_code1)\n",
    "device_code1_st = device_code1_gb[tag_hd.UID].count().reset_index()\n",
    "device_code1_st.columns = ['device_code1','device_code1_uid_cnt']\n",
    "device_code1_st['device_code1_op_cnt_rate'] =device_code1_st['device_code1_uid_cnt']/op_train.shape[0]\n",
    "device_code1_st02 = device_code1_gb[tag_hd.UID].nunique().reset_index()\n",
    "device_code1_st02.columns = ['device_code1','device_code1_uid_nunique']\n",
    "device_code1_st02['device_code1_op_nunique_rate'] =device_code1_st['device_code1_uid_cnt']/device_code1_st02['device_code1_uid_nunique'].sum()\n",
    "device_code1_st = device_code1_st.merge(device_code1_st02, on=op_hd.device_code1, how='left')\n",
    "device_code1_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>device_code2</th>\n",
       "      <th>device_code2_uid_cnt</th>\n",
       "      <th>device_code2_op_cnt_rate</th>\n",
       "      <th>device_code2_uid_nunique</th>\n",
       "      <th>device_code2_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00002ed76eb9d313</td>\n",
       "      <td>133</td>\n",
       "      <td>0.000091</td>\n",
       "      <td>1</td>\n",
       "      <td>0.001528</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       device_code2  device_code2_uid_cnt  device_code2_op_cnt_rate  \\\n",
       "0  00002ed76eb9d313                   133                  0.000091   \n",
       "\n",
       "   device_code2_uid_nunique  device_code2_op_nunique_rate  \n",
       "0                         1                      0.001528  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "device_code2_gb = op_merge.groupby(op_hd.device_code2)\n",
    "device_code2_st = device_code2_gb[tag_hd.UID].count().reset_index()\n",
    "device_code2_st.columns = ['device_code2','device_code2_uid_cnt']\n",
    "device_code2_st['device_code2_op_cnt_rate'] =device_code2_st['device_code2_uid_cnt']/op_train.shape[0]\n",
    "device_code2_st02 = device_code2_gb[tag_hd.UID].nunique().reset_index()\n",
    "device_code2_st02.columns = ['device_code2','device_code2_uid_nunique']\n",
    "device_code2_st02['device_code2_op_nunique_rate'] =device_code2_st['device_code2_uid_cnt']/device_code2_st02['device_code2_uid_nunique'].sum()\n",
    "device_code2_st = device_code2_st.merge(device_code2_st02, on=op_hd.device_code2, how='left')\n",
    "device_code2_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mac1</th>\n",
       "      <th>mac1_uid_cnt</th>\n",
       "      <th>mac1_op_cnt_rate</th>\n",
       "      <th>mac1_uid_nunique</th>\n",
       "      <th>mac1_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00009922206bab3d</td>\n",
       "      <td>26</td>\n",
       "      <td>0.000018</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               mac1  mac1_uid_cnt  mac1_op_cnt_rate  mac1_uid_nunique  \\\n",
       "0  00009922206bab3d            26          0.000018                 2   \n",
       "\n",
       "   mac1_op_nunique_rate  \n",
       "0              0.000285  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mac1_gb = op_merge.groupby(op_hd.mac1)\n",
    "mac1_st = mac1_gb[tag_hd.UID].count().reset_index()\n",
    "mac1_st.columns = ['mac1','mac1_uid_cnt']\n",
    "mac1_st['mac1_op_cnt_rate'] =mac1_st['mac1_uid_cnt']/op_train.shape[0]\n",
    "mac1_st02 = mac1_gb[tag_hd.UID].nunique().reset_index()\n",
    "mac1_st02.columns = ['mac1','mac1_uid_nunique']\n",
    "mac1_st02['mac1_op_nunique_rate'] =mac1_st['mac1_uid_cnt']/mac1_st02['mac1_uid_nunique'].sum()\n",
    "mac1_st = mac1_st.merge(mac1_st02, on=op_hd.mac1, how='left')\n",
    "mac1_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ip1</th>\n",
       "      <th>ip1_uid_cnt</th>\n",
       "      <th>ip1_op_cnt_rate</th>\n",
       "      <th>ip1_uid_nunique</th>\n",
       "      <th>ip1_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000002e13d545e71</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                ip1  ip1_uid_cnt  ip1_op_cnt_rate  ip1_uid_nunique  \\\n",
       "0  000002e13d545e71            3         0.000002                1   \n",
       "\n",
       "   ip1_op_nunique_rate  \n",
       "0             0.000008  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ip1_gb = op_merge.groupby(op_hd.ip1)\n",
    "ip1_st = ip1_gb[tag_hd.UID].count().reset_index()\n",
    "ip1_st.columns = ['ip1','ip1_uid_cnt']\n",
    "ip1_st['ip1_op_cnt_rate'] =ip1_st['ip1_uid_cnt']/op_train.shape[0]\n",
    "ip1_st02 = ip1_gb[tag_hd.UID].nunique().reset_index()\n",
    "ip1_st02.columns = ['ip1','ip1_uid_nunique']\n",
    "ip1_st02['ip1_op_nunique_rate'] =ip1_st['ip1_uid_cnt']/ip1_st02['ip1_uid_nunique'].sum()\n",
    "ip1_st = ip1_st.merge(ip1_st02, on=op_hd.ip1, how='left')\n",
    "ip1_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ip2</th>\n",
       "      <th>ip2_uid_cnt</th>\n",
       "      <th>ip2_op_cnt_rate</th>\n",
       "      <th>ip2_uid_nunique</th>\n",
       "      <th>ip2_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000002e13d545e71</td>\n",
       "      <td>15</td>\n",
       "      <td>0.00001</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000134</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                ip2  ip2_uid_cnt  ip2_op_cnt_rate  ip2_uid_nunique  \\\n",
       "0  000002e13d545e71           15          0.00001                1   \n",
       "\n",
       "   ip2_op_nunique_rate  \n",
       "0             0.000134  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ip2_gb = op_merge.groupby(op_hd.ip2)\n",
    "ip2_st = ip2_gb[tag_hd.UID].count().reset_index()\n",
    "ip2_st.columns = ['ip2','ip2_uid_cnt']\n",
    "ip2_st['ip2_op_cnt_rate'] =ip2_st['ip2_uid_cnt']/op_train.shape[0]\n",
    "ip2_st02 = ip2_gb[tag_hd.UID].nunique().reset_index()\n",
    "ip2_st02.columns = ['ip2','ip2_uid_nunique']\n",
    "ip2_st02['ip2_op_nunique_rate'] =ip2_st['ip2_uid_cnt']/ip2_st02['ip2_uid_nunique'].sum()\n",
    "ip2_st = ip2_st.merge(ip2_st02, on=op_hd.ip2, how='left')\n",
    "ip2_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>device_code3</th>\n",
       "      <th>device_code3_uid_cnt</th>\n",
       "      <th>device_code3_op_cnt_rate</th>\n",
       "      <th>device_code3_uid_nunique</th>\n",
       "      <th>device_code3_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0002730db9a8d576</td>\n",
       "      <td>117</td>\n",
       "      <td>0.00008</td>\n",
       "      <td>4</td>\n",
       "      <td>0.001713</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       device_code3  device_code3_uid_cnt  device_code3_op_cnt_rate  \\\n",
       "0  0002730db9a8d576                   117                   0.00008   \n",
       "\n",
       "   device_code3_uid_nunique  device_code3_op_nunique_rate  \n",
       "0                         4                      0.001713  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#设备号唯一标识加密，可用于苹果类设备的唯一标识\n",
    "device_code3_gb = op_merge.groupby(op_hd.device_code3)\n",
    "device_code3_st = device_code3_gb[tag_hd.UID].count().reset_index()\n",
    "device_code3_st.columns = ['device_code3','device_code3_uid_cnt']\n",
    "device_code3_st['device_code3_op_cnt_rate'] =device_code3_st['device_code3_uid_cnt']/op_train.shape[0]\n",
    "device_code3_st02 = device_code3_gb[tag_hd.UID].nunique().reset_index()\n",
    "device_code3_st02.columns = ['device_code3','device_code3_uid_nunique']\n",
    "device_code3_st02['device_code3_op_nunique_rate'] =device_code3_st['device_code3_uid_cnt']/device_code3_st02['device_code3_uid_nunique'].sum()\n",
    "device_code3_st = device_code3_st.merge(device_code3_st02, on=op_hd.device_code3, how='left')\n",
    "device_code3_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mac2</th>\n",
       "      <th>mac2_uid_cnt</th>\n",
       "      <th>mac2_op_cnt_rate</th>\n",
       "      <th>mac2_uid_nunique</th>\n",
       "      <th>mac2_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000076e2bcc92ce7</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000032</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               mac2  mac2_uid_cnt  mac2_op_cnt_rate  mac2_uid_nunique  \\\n",
       "0  000076e2bcc92ce7             3          0.000002                 1   \n",
       "\n",
       "   mac2_op_nunique_rate  \n",
       "0              0.000032  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mac2_gb = op_merge.groupby(op_hd.mac2)\n",
    "mac2_st = mac2_gb[tag_hd.UID].count().reset_index()\n",
    "mac2_st.columns = ['mac2','mac2_uid_cnt']\n",
    "mac2_st['mac2_op_cnt_rate'] =mac2_st['mac2_uid_cnt']/op_train.shape[0]\n",
    "mac2_st02 = mac2_gb[tag_hd.UID].nunique().reset_index()\n",
    "mac2_st02.columns = ['mac2','mac2_uid_nunique']\n",
    "mac2_st02['mac2_op_nunique_rate'] =mac2_st['mac2_uid_cnt']/mac2_st02['mac2_uid_nunique'].sum()\n",
    "mac2_st = mac2_st.merge(mac2_st02, on=op_hd.mac2, how='left')\n",
    "mac2_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>wifi</th>\n",
       "      <th>wifi_uid_cnt</th>\n",
       "      <th>wifi_op_cnt_rate</th>\n",
       "      <th>wifi_uid_nunique</th>\n",
       "      <th>wifi_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00012a6a0f6a60a8</td>\n",
       "      <td>25</td>\n",
       "      <td>0.000017</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000206</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               wifi  wifi_uid_cnt  wifi_op_cnt_rate  wifi_uid_nunique  \\\n",
       "0  00012a6a0f6a60a8            25          0.000017                 1   \n",
       "\n",
       "   wifi_op_nunique_rate  \n",
       "0              0.000206  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wifi_gb = op_merge.groupby(op_hd.wifi)\n",
    "wifi_st = wifi_gb[tag_hd.UID].count().reset_index()\n",
    "wifi_st.columns = ['wifi','wifi_uid_cnt']\n",
    "wifi_st['wifi_op_cnt_rate'] =wifi_st['wifi_uid_cnt']/op_train.shape[0]\n",
    "wifi_st02 = wifi_gb[tag_hd.UID].nunique().reset_index()\n",
    "wifi_st02.columns = ['wifi','wifi_uid_nunique']\n",
    "wifi_st02['wifi_op_nunique_rate'] =wifi_st['wifi_uid_cnt']/wifi_st02['wifi_uid_nunique'].sum()\n",
    "wifi_st = wifi_st.merge(wifi_st02, on=op_hd.wifi, how='left')\n",
    "wifi_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geo_code</th>\n",
       "      <th>geo_code_uid_cnt</th>\n",
       "      <th>geo_code_op_cnt_rate</th>\n",
       "      <th>geo_code_uid_nunique</th>\n",
       "      <th>geo_code_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7zzz</td>\n",
       "      <td>20</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000206</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  geo_code  geo_code_uid_cnt  geo_code_op_cnt_rate  geo_code_uid_nunique  \\\n",
       "0     7zzz                20              0.000014                     4   \n",
       "\n",
       "   geo_code_op_nunique_rate  \n",
       "0                  0.000206  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geo_code_gb = op_merge.groupby(op_hd.geo_code)\n",
    "geo_code_st = geo_code_gb[tag_hd.UID].count().reset_index()\n",
    "geo_code_st.columns = ['geo_code','geo_code_uid_cnt']\n",
    "geo_code_st['geo_code_op_cnt_rate'] =geo_code_st['geo_code_uid_cnt']/op_train.shape[0]\n",
    "geo_code_st02 = geo_code_gb[tag_hd.UID].nunique().reset_index()\n",
    "geo_code_st02.columns = ['geo_code','geo_code_uid_nunique']\n",
    "geo_code_st02['geo_code_op_nunique_rate'] =geo_code_st['geo_code_uid_cnt']/geo_code_st02['geo_code_uid_nunique'].sum()\n",
    "geo_code_st = geo_code_st.merge(geo_code_st02, on=op_hd.geo_code, how='left')\n",
    "geo_code_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ip1_sub</th>\n",
       "      <th>ip1_sub_uid_cnt</th>\n",
       "      <th>ip1_sub_op_cnt_rate</th>\n",
       "      <th>ip1_sub_uid_nunique</th>\n",
       "      <th>ip1_sub_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00030f573c9b9140</td>\n",
       "      <td>1137</td>\n",
       "      <td>0.000778</td>\n",
       "      <td>151</td>\n",
       "      <td>0.003431</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            ip1_sub  ip1_sub_uid_cnt  ip1_sub_op_cnt_rate  \\\n",
       "0  00030f573c9b9140             1137             0.000778   \n",
       "\n",
       "   ip1_sub_uid_nunique  ip1_sub_op_nunique_rate  \n",
       "0                  151                 0.003431  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 前三位操作设备IP地址编码加密(ip1前三位IP地址)\n",
    "# 比如，原字段为12,34,56,7和12,34,56,8的ip地址前三位都为12,34,56，故脱敏后的值是一样的\n",
    "ip1_sub_gb = op_merge.groupby(op_hd.ip1_sub)\n",
    "ip1_sub_st = ip1_sub_gb[tag_hd.UID].count().reset_index()\n",
    "ip1_sub_st.columns = ['ip1_sub','ip1_sub_uid_cnt']\n",
    "ip1_sub_st['ip1_sub_op_cnt_rate'] =ip1_sub_st['ip1_sub_uid_cnt']/op_train.shape[0]\n",
    "ip1_sub_st02 = ip1_sub_gb[tag_hd.UID].nunique().reset_index()\n",
    "ip1_sub_st02.columns = ['ip1_sub','ip1_sub_uid_nunique']\n",
    "ip1_sub_st02['ip1_sub_op_nunique_rate'] =ip1_sub_st['ip1_sub_uid_cnt']/ip1_sub_st02['ip1_sub_uid_nunique'].sum()\n",
    "ip1_sub_st = ip1_sub_st.merge(ip1_sub_st02, on=op_hd.ip1_sub, how='left')\n",
    "ip1_sub_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ip2_sub</th>\n",
       "      <th>ip2_sub_uid_cnt</th>\n",
       "      <th>ip2_sub_op_cnt_rate</th>\n",
       "      <th>ip2_sub_uid_nunique</th>\n",
       "      <th>ip2_sub_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00030f573c9b9140</td>\n",
       "      <td>535</td>\n",
       "      <td>0.000366</td>\n",
       "      <td>25</td>\n",
       "      <td>0.005099</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            ip2_sub  ip2_sub_uid_cnt  ip2_sub_op_cnt_rate  \\\n",
       "0  00030f573c9b9140              535             0.000366   \n",
       "\n",
       "   ip2_sub_uid_nunique  ip2_sub_op_nunique_rate  \n",
       "0                   25                 0.005099  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ip2_sub_gb = op_merge.groupby(op_hd.ip2_sub)\n",
    "ip2_sub_st = ip2_sub_gb[tag_hd.UID].count().reset_index()\n",
    "ip2_sub_st.columns = ['ip2_sub','ip2_sub_uid_cnt']\n",
    "ip2_sub_st['ip2_sub_op_cnt_rate'] =ip2_sub_st['ip2_sub_uid_cnt']/op_train.shape[0]\n",
    "ip2_sub_st02 = ip2_sub_gb[tag_hd.UID].nunique().reset_index()\n",
    "ip2_sub_st02.columns = ['ip2_sub','ip2_sub_uid_nunique']\n",
    "ip2_sub_st02['ip2_sub_op_nunique_rate'] =ip2_sub_st['ip2_sub_uid_cnt']/ip2_sub_st02['ip2_sub_uid_nunique'].sum()\n",
    "ip2_sub_st = ip2_sub_st.merge(ip2_sub_st02, on=op_hd.ip2_sub, how='left')\n",
    "ip2_sub_st.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UID</th>\n",
       "      <th>day</th>\n",
       "      <th>mode</th>\n",
       "      <th>success</th>\n",
       "      <th>time</th>\n",
       "      <th>os</th>\n",
       "      <th>version</th>\n",
       "      <th>device1</th>\n",
       "      <th>device2</th>\n",
       "      <th>device_code1</th>\n",
       "      <th>...</th>\n",
       "      <th>geo_code_uid_nunique</th>\n",
       "      <th>geo_code_op_nunique_rate</th>\n",
       "      <th>ip1_sub_uid_cnt</th>\n",
       "      <th>ip1_sub_op_cnt_rate</th>\n",
       "      <th>ip1_sub_uid_nunique</th>\n",
       "      <th>ip1_sub_op_nunique_rate</th>\n",
       "      <th>ip2_sub_uid_cnt</th>\n",
       "      <th>ip2_sub_op_cnt_rate</th>\n",
       "      <th>ip2_sub_uid_nunique</th>\n",
       "      <th>ip2_sub_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10000</td>\n",
       "      <td>13.0</td>\n",
       "      <td>c8741ce15ceac2a4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14</td>\n",
       "      <td>103.0</td>\n",
       "      <td>7.0.5</td>\n",
       "      <td>aca4977fbe8741e1</td>\n",
       "      <td>IPHONE 5</td>\n",
       "      <td>e56819f72c9b7860</td>\n",
       "      <td>...</td>\n",
       "      <td>56</td>\n",
       "      <td>0.027239</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000003</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000012</td>\n",
       "      <td>291</td>\n",
       "      <td>0.000199</td>\n",
       "      <td>13</td>\n",
       "      <td>0.002773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10000</td>\n",
       "      <td>26.0</td>\n",
       "      <td>c8741ce15ceac2a4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>12</td>\n",
       "      <td>103.0</td>\n",
       "      <td>7.0.7</td>\n",
       "      <td>aca4977fbe8741e1</td>\n",
       "      <td>IPHONE 5</td>\n",
       "      <td>e56819f72c9b7860</td>\n",
       "      <td>...</td>\n",
       "      <td>56</td>\n",
       "      <td>0.027239</td>\n",
       "      <td>9</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>291</td>\n",
       "      <td>0.000199</td>\n",
       "      <td>13</td>\n",
       "      <td>0.002773</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 97 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     UID   day              mode  success time     os version  \\\n",
       "0  10000  13.0  c8741ce15ceac2a4      1.0   14  103.0   7.0.5   \n",
       "1  10000  26.0  c8741ce15ceac2a4      1.0   12  103.0   7.0.7   \n",
       "\n",
       "            device1   device2      device_code1           ...            \\\n",
       "0  aca4977fbe8741e1  IPHONE 5  e56819f72c9b7860           ...             \n",
       "1  aca4977fbe8741e1  IPHONE 5  e56819f72c9b7860           ...             \n",
       "\n",
       "  geo_code_uid_nunique geo_code_op_nunique_rate ip1_sub_uid_cnt  \\\n",
       "0                   56                 0.027239               4   \n",
       "1                   56                 0.027239               9   \n",
       "\n",
       "  ip1_sub_op_cnt_rate ip1_sub_uid_nunique ip1_sub_op_nunique_rate  \\\n",
       "0            0.000003                   1                0.000012   \n",
       "1            0.000006                   2                0.000027   \n",
       "\n",
       "  ip2_sub_uid_cnt ip2_sub_op_cnt_rate ip2_sub_uid_nunique  \\\n",
       "0             291            0.000199                  13   \n",
       "1             291            0.000199                  13   \n",
       "\n",
       "  ip2_sub_op_nunique_rate  \n",
       "0                0.002773  \n",
       "1                0.002773  \n",
       "\n",
       "[2 rows x 97 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# op_merge = op_merge.merge(day_st, on=op_hd.day, how='left')\n",
    "op_merge = op_merge.merge(mode_st, on=op_hd.mode, how='left')\n",
    "op_merge = op_merge.merge(success_st, on=op_hd.success, how='left')\n",
    "op_merge = op_merge.merge(time_st, on=op_hd.time, how='left')\n",
    "op_merge = op_merge.merge(os_st, on=op_hd.os, how='left')\n",
    "op_merge = op_merge.merge(version_st, on=op_hd.version, how='left')\n",
    "op_merge = op_merge.merge(device1_st, on=op_hd.device1, how='left')\n",
    "op_merge = op_merge.merge(device2_st, on=op_hd.device2, how='left')\n",
    "op_merge = op_merge.merge(device_code1_st, on=op_hd.device_code1, how='left')\n",
    "op_merge = op_merge.merge(device_code2_st, on=op_hd.device_code2, how='left')\n",
    "op_merge = op_merge.merge(mac1_st, on=op_hd.mac1, how='left')\n",
    "op_merge = op_merge.merge(ip1_st, on=op_hd.ip1, how='left')\n",
    "op_merge = op_merge.merge(ip2_st, on=op_hd.ip2, how='left')\n",
    "op_merge = op_merge.merge(device_code3_st, on=op_hd.device_code3, how='left')\n",
    "op_merge = op_merge.merge(mac2_st, on=op_hd.mac2, how='left')\n",
    "op_merge = op_merge.merge(wifi_st, on=op_hd.wifi, how='left')\n",
    "op_merge = op_merge.merge(geo_code_st, on=op_hd.geo_code, how='left')\n",
    "op_merge = op_merge.merge(ip1_sub_st, on=op_hd.ip1_sub, how='left')\n",
    "op_merge = op_merge.merge(ip2_sub_st, on=op_hd.ip2_sub, how='left')\n",
    "op_merge.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mode',\n",
       " 'time',\n",
       " 'version',\n",
       " 'device1',\n",
       " 'device2',\n",
       " 'device_code1',\n",
       " 'device_code2',\n",
       " 'device_code3',\n",
       " 'mac1',\n",
       " 'mac2',\n",
       " 'ip1',\n",
       " 'ip2',\n",
       " 'wifi',\n",
       " 'geo_code',\n",
       " 'ip1_sub',\n",
       " 'ip2_sub']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "object_col = op_merge.select_dtypes(['object']).columns.values.tolist()\n",
    "object_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['device_code3',\n",
       " 'mac2',\n",
       " 'ip2',\n",
       " 'success_op_cnt_rate',\n",
       " 'success_uid_cnt',\n",
       " 'day_uid_cnt',\n",
       " 'os_uid_cnt',\n",
       " 'device_code1_uid_nunique',\n",
       " 'version',\n",
       " 'device2',\n",
       " 'mac1',\n",
       " 'success_op_nunique_rate',\n",
       " 'version_op_cnt_rate',\n",
       " 'ip2_sub',\n",
       " 'time_op_nunique_rate',\n",
       " 'time_uid_nunique',\n",
       " 'day',\n",
       " 'device_code2_uid_nunique',\n",
       " 'mode',\n",
       " 'os_uid_nunique',\n",
       " 'Tag',\n",
       " 'wifi',\n",
       " 'time_uid_cnt',\n",
       " 'device_code2',\n",
       " 'device_code1',\n",
       " 'version_op_nunique_rate',\n",
       " 'device1',\n",
       " 'ip1_sub',\n",
       " 'ip1',\n",
       " 'day_op_cnt_rate',\n",
       " 'os_op_nunique_rate',\n",
       " 'time',\n",
       " 'day_uid_nunique',\n",
       " 'time_op_cnt_rate',\n",
       " 'version_uid_cnt',\n",
       " 'os',\n",
       " 'os_op_cnt_rate',\n",
       " 'day_op_nunique_rate',\n",
       " 'success_uid_nunique',\n",
       " 'success',\n",
       " 'version_uid_nunique',\n",
       " 'geo_code']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "op_merge_nunique = op_merge.nunique()\n",
    "op_merge_nunique.sort_values(inplace=True)\n",
    "one_hot_col = op_merge_nunique[op_merge_nunique < 50].index.values.tolist() \n",
    "one_hot_col = list(set(one_hot_col) | set(object_col))\n",
    "one_hot_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3234654, 81)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UID</th>\n",
       "      <th>day</th>\n",
       "      <th>success</th>\n",
       "      <th>os</th>\n",
       "      <th>Tag</th>\n",
       "      <th>day_uid_cnt</th>\n",
       "      <th>day_op_cnt_rate</th>\n",
       "      <th>day_uid_nunique</th>\n",
       "      <th>day_op_nunique_rate</th>\n",
       "      <th>mode_uid_cnt</th>\n",
       "      <th>...</th>\n",
       "      <th>geo_code_uid_nunique</th>\n",
       "      <th>geo_code_op_nunique_rate</th>\n",
       "      <th>ip1_sub_uid_cnt</th>\n",
       "      <th>ip1_sub_op_cnt_rate</th>\n",
       "      <th>ip1_sub_uid_nunique</th>\n",
       "      <th>ip1_sub_op_nunique_rate</th>\n",
       "      <th>ip2_sub_uid_cnt</th>\n",
       "      <th>ip2_sub_op_cnt_rate</th>\n",
       "      <th>ip2_sub_uid_nunique</th>\n",
       "      <th>ip2_sub_op_nunique_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10000</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>103.0</td>\n",
       "      <td>1</td>\n",
       "      <td>100353</td>\n",
       "      <td>0.068627</td>\n",
       "      <td>10970</td>\n",
       "      <td>0.285886</td>\n",
       "      <td>1776471</td>\n",
       "      <td>...</td>\n",
       "      <td>56</td>\n",
       "      <td>0.027239</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000003</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000012</td>\n",
       "      <td>291</td>\n",
       "      <td>0.000199</td>\n",
       "      <td>13</td>\n",
       "      <td>0.002773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10000</td>\n",
       "      <td>26.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>103.0</td>\n",
       "      <td>1</td>\n",
       "      <td>81604</td>\n",
       "      <td>0.055805</td>\n",
       "      <td>9367</td>\n",
       "      <td>0.232474</td>\n",
       "      <td>1776471</td>\n",
       "      <td>...</td>\n",
       "      <td>56</td>\n",
       "      <td>0.027239</td>\n",
       "      <td>9</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>291</td>\n",
       "      <td>0.000199</td>\n",
       "      <td>13</td>\n",
       "      <td>0.002773</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 81 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     UID   day  success     os  Tag  day_uid_cnt  day_op_cnt_rate  \\\n",
       "0  10000  13.0      1.0  103.0    1       100353         0.068627   \n",
       "1  10000  26.0      1.0  103.0    1        81604         0.055805   \n",
       "\n",
       "   day_uid_nunique  day_op_nunique_rate  mode_uid_cnt  \\\n",
       "0            10970             0.285886       1776471   \n",
       "1             9367             0.232474       1776471   \n",
       "\n",
       "            ...             geo_code_uid_nunique  geo_code_op_nunique_rate  \\\n",
       "0           ...                               56                  0.027239   \n",
       "1           ...                               56                  0.027239   \n",
       "\n",
       "   ip1_sub_uid_cnt  ip1_sub_op_cnt_rate  ip1_sub_uid_nunique  \\\n",
       "0                4             0.000003                    1   \n",
       "1                9             0.000006                    2   \n",
       "\n",
       "   ip1_sub_op_nunique_rate  ip2_sub_uid_cnt  ip2_sub_op_cnt_rate  \\\n",
       "0                 0.000012              291             0.000199   \n",
       "1                 0.000027              291             0.000199   \n",
       "\n",
       "   ip2_sub_uid_nunique  ip2_sub_op_nunique_rate  \n",
       "0                   13                 0.002773  \n",
       "1                   13                 0.002773  \n",
       "\n",
       "[2 rows x 81 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# one_hot_col.remove('Tag')\n",
    "# op_merge = op_merge.drop(object_col, axis=1)\n",
    "print(op_merge.shape)\n",
    "op_merge.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "op_merge.to_csv(data_base_path+ 'op_merges.csv', index=False)\n",
    "print(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "day\n",
      "success\n",
      "os\n",
      "day_uid_cnt\n",
      "day_op_cnt_rate\n",
      "day_uid_nunique\n",
      "day_op_nunique_rate\n",
      "mode_uid_cnt\n",
      "mode_op_cnt_rate\n",
      "mode_uid_nunique\n",
      "mode_op_nunique_rate\n",
      "success_uid_cnt\n",
      "success_op_cnt_rate\n",
      "success_uid_nunique\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def get_features(data):\n",
    "    label = data[tag_header]\n",
    "    uid_gb = data.groupby(['UID'])\n",
    "    cols = data.columns.values.tolist()\n",
    "    cols.remove(tag_hd.Tag)\n",
    "    cols.remove(tag_hd.UID)\n",
    "    for feature in cols:\n",
    "        print(feature)\n",
    "        label = label.merge(uid_gb[feature].count().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].nunique().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].max().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].min().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].sum().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].mean().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].std().reset_index(), on='UID', how='left')\n",
    "        label = label.merge(uid_gb[feature].var().reset_index(), on='UID', how='left')\n",
    "    return label\n",
    "opmerge_ftrs = get_features(op_merge)\n",
    "opmerge_ftrs.to_csv(features_base_path+ 'opmerge_ftrs.csv', index=False)\n",
    "print(opmerge_ftrs.shape)\n",
    "opmerge_ftrs.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%hist -f merge_op_data_analysis.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
